var util = require("../../utils/util.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        addressList: [],
        goodDetailId: -1,
        num: '',
        addressIndex:-1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            type: options.type,
            id: options.id,
            count: options.count,
            pickNum: options.pickNum,
            addIndex: options.index,
            time: options.time
        });
        this.getAddressList();
        //传地址的下标，购买次数为一次是 就传数组下标1
        if(options.addressIndex || options.addressIndex==0){
            this.setData({
                addressIndex: options.addressIndex
            });
        }
        if(this.data.pickNum!='undefined'){
            wx.setNavigationBarTitle({
                title: '编辑地址'
              })
            this.setData({
                addFlag: !0
            });
        }
    },
    chooseAddress: function(e) {
        var addressInfo = e.currentTarget.dataset.info;
        var index = e.currentTarget.dataset.index;
        var pickNum = this.data.pickNum;
        var addIndex = this.data.addIndex;
        var time = this.data.time;
        if(addressInfo.id == this.data.id && this.data.type=='card'){
            this.goToPrevPage(addressInfo,index,pickNum,addIndex,time);
        }else{
            this.goToPrevPage(addressInfo,index);
        }

    },
    editAddress: function(e) {
        var addressInfo = e.currentTarget.dataset.info;
        this.goToPrevPage(addressInfo);
    },
    goToPrevPage:function(addressInfo,index,pickNum,addIndex,time){
        // console.log(6,this.data.addressList,index)
        if(this.data.type=='card'){
            wx.navigateTo({
                url: "/pages/delivery-time/delivery-time?index=" + index + '&pickNum=' + pickNum +'&addIndex=' + addIndex + '&time=' + time
            })
        }else{
            console.log('this.data.addressIndex',this.data.addressIndex);
            var pages = getCurrentPages();//当前页面
            var prevPage = pages[pages.length - 2];//上一页面
            let addressList = prevPage.data.addressList||[];
            if(addressList.length>0){
                for(let t in addressInfo){
                    addressList[this.data.addressIndex][t] = addressInfo[t]
                }
                console.log(".................",addressList[this.data.addressIndex]);
            }else{
                addressList.push(addressInfo);
            }
            prevPage.setData({//直接给上移页面赋值
                addressList: addressList,
                selAddress: 'yes'
            });
            if(this.data.count>1){
                wx.navigateTo({
                    url: "/pages/delivery-ordertime/delivery-ordertime?index=" + this.data.addressIndex
                })
            }else{
                wx.navigateBack({//返回
                    delta: 1
                })
            }
        }
    },
    toPrevPage:function(){
        //返回上一页，并重刷选择地址列表
        var pages = getCurrentPages();//当前页面
        var prevPage = pages[pages.length - 2];//上一页面
        prevPage.changeData();
        wx.navigateBack({//返回
            delta: 1
        })
    },
    changeData:function(){
        this.getAddressList();
    },
    getAddressList: function() {
        console.log(111)
        let _this = this;
        util.get(
            getApp().api.address.list
        ).then(
            function(res) {
                if (res.code == 200) {
                    _this.setData({
                        addressList: res.data,
                    });
                } else {
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        icon: "none", //图标,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function() {
                        wx.hideToast();
                    }, 1500);
                    return
                }

            }
        )
    },
    toAddressEdit: function(e) {
        var item = e.currentTarget.dataset.info;
        wx.navigateTo({
            url: "/pages/address-edit/address-edit?addressId="+item.id
        })
    },
    toAddressAdd: function() {
        let _this = this;
        wx.navigateTo({
            url: "/pages/address-edit/address-edit?add=1"
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {
        //返回上一页，将addressList页面里面的push的数据清空
        var pages = getCurrentPages();//当前页面
        var prevPage = pages[pages.length - 2];//上一页面
        if(prevPage.data){
            let addressList = prevPage.data.addressList;
            if (Array.isArray(addressList)){
                if(addressList.length - 1 == this.data.addressIndex && !addressList[this.data.addressIndex].deliveryName){
                    addressList.pop();
                }
                prevPage.setData({//直接给上移页面赋值
                    addressList: addressList
                });
            }
        }
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})